<%@ page isELIgnored="false" language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" %>


<script type="text/javascript">
    $(function(){
        /*加载登录对话框设置加载时隐藏属性  */
        $("#addDialog").dialog({
            title: "添加框",
            width: 600,
            height: 300,
            closed: true,
            buttons:[{
                text:"添加",
                handler:function(){
                    doadd();
                },
            }],
        });
        /*加载修改对话框设置加载时隐藏属性---------------END  */
        $("#updateDialog").dialog({
            title: "修改框",
            width: 600,
            height: 500,
            closed: true,
            buttons:[{
                text:"修改",
                handler:function(){
                    doupdate();
                },
            }],
        });
        /*加载修改对话框设置加载时隐藏属性---------------END  */



       $("#allBanner").datagrid({
           fit:"true",
           pagination:"true",
           url:"${pageContext.request.contextPath}/Banner/selectAllBanner.do",
           columns:[[
               {field:'bannerId',checkbox:"true"},
               {field:'bannerImageUrl',title:'图片',width:100,formatter: function(value,row,index){
                   return "<img style='width:32px;height:32px;' border='1' src='${pageContext.request.contextPath}"+row.bannerImageUrl+"'/>";
                   }
               },
               {field:'bannerOldName',title:'标题名称',width:100},
               {field:'bannerDescription',title:'是否显示',width:100,
                    formatter: function(value,row,index){
                        if (row.bannerDescription==0){
                            return "显示";
                        } else {
                            return "不显示";
                        }
                    }
                 },
               {field:'bannerDate',title:'上传时间',width:100},
           ]],
           toolbar: [{
               /*添加工具  */
               text: "添加",
               iconCls: 'icon-add',
               handler: function () {
                   /*打开添加对话框  */
                   $("#addDialog").panel('open');
               },
               /*添加工具-----------end  */
           },{
               iconCls: 'icon-help',
               text:'删除选中',
               handler: function() {
                   $.messager.confirm("确认框", "您确认想要删除记录吗？", function (r) {
                       if (r) {
                           /*获取选中的行对象  */
                           var hang = $("#allBanner").datagrid("getSelections");
                           var ids = new Array();
                           for (var i = 0; i < hang.length; i++) {
                               ids[i] = hang[i].bannerId;
                           }
                           /*重新发送ajax请求删除选中的id集合  */
                           $.ajax({
                               url: "${pageContext.request.contextPath}/Banner/delatebanner.do",
                               data: {"ids": ids},
                               type: "post",
                               /*提示ajax不做深度解析  */
                               traditional: true,
                               success: function (data) {
                                   if (data.isok) {
                                       $.messager.alert("消息", "删除成功");
                                       $("#allBanner").datagrid("reload");

                                   } else {
                                       $.messager.alert("消息", "删除失败");
                                       $("#allBanner").datagrid("reload");
                                   }
                               }
                           });
                       }
                   });
               }
           }],
           onDblClickRow: function (index, rowDate) {
              $("#updateDialog").panel('open');
               $("#bannerId").val(rowDate.bannerId);
               $("#bannerImageUrl").val(rowDate.bannerImageUrl);
               $("#bannerImageEcho2").prop("src","${pageContext.request.contextPath}"+rowDate.bannerImageUrl);
              $("#bannerOldName2").val(rowDate.bannerOldName);
                 if(rowDate.bannerDescription==0){
                    $("#radio1").prop("checked",true);
                }else {
                    $("#radio2").prop("checked",true);
                }
               console.log(rowDate);
           },
       });
    });



    function doadd(){
        $("#addform").form("submit",{
            url:"${pageContext.request.contextPath}/Banner/addBanner.do",
            onSubmit: function(){
                //体检数据前需要的判断
                return true;
            },
            success:function(data){
                var data = eval('(' + data + ')');
                if(data.isok){
                    /*添加成功弹出对话框  */
                    $.messager.alert("消息","添加成功");
                    /*关闭对话框*/
                    $("#addDialog").dialog('close');
                    /*刷新table  */
                    $("#allBanner").datagrid("reload");
                }else{
                    $.messager.alert("消息","添加失败");
                };
            }

        });
    }
    /*定义点击添加对话框中的登录按钮的点击事件-----------------END  */


    /*设置修改的点击事件  */
    function doupdate(){
        $("#updateform").form("submit",{
            url:"${pageContext.request.contextPath}/Banner/updateBanner.do",
            onSubmit: function(){
                return true;
            },
            success:function(data){
                var data = eval('(' + data + ')');  // change the JSON string to javascript object
                if(data.isok){
                    $.messager.alert("消息","修改成功");
                    /*刷新table  */
                    $("#updateDialog").dialog("close",true);
                    $("#allBanner").datagrid("reload");
                }else{
                    $.messager.alert("消息","修改失败");
                };
            },

        });
    }
    /*定义点击修改对话框中的点击事件-----------------END  */



</script>


<script type="text/javascript">
    var image = '';
    function selectImage(file) {
        if (!file.files || !file.files[0]) {
            return;
        }
        var reader = new FileReader();
        reader.onload = function (evt) {
            document.getElementById('bannerImageEcho').src = evt.target.result;
            image = evt.target.result;
        }
        reader.readAsDataURL(file.files[0]);
    }
</script>
<script type="text/javascript">
    var image = '';
    function selectImage2(file) {
        if (!file.files || !file.files[0]) {
            return;
        }
        var reader = new FileReader();
        reader.onload = function (evt) {
            document.getElementById('bannerImageEcho2').src = evt.target.result;
            image = evt.target.result;
        }
        reader.readAsDataURL(file.files[0]);
    }
</script>



<table id="allBanner" class="easyui-datagrid" style="width:400px;height:250px">

</table>


<!--添加的对话框表  -->
<div id="addDialog" >
    <form id="addform" enctype="multipart/form-data" method="post">
        图片：<input type="file" value="" name="bannerImage" onchange="selectImage(this);">
        <div class="lf salebd">
            <a href="#"><img id="bannerImageEcho" src="images/ad20.jpg" width="100" height="100" /></a>
        </div><br/>
        标题：<input id="bannerOldName" name="bannerOldName"><br/>
        是否显示：<br/>显示<input type="radio" value="0" name="bannerDescription"><br/>
        不显示<input type="radio" value="1" name="bannerDescription"><br/>
    </form>
</div>
<!--添加的对话框表 -----------end -->


<!--修改的对话框  -->
<div id="updateDialog" >
    <form id="updateform" method="post"  enctype="multipart/form-data" class="easyui-form" data-options="novalidate:true">
        <input id="bannerId"  name="bannerId" style="display: none">
        <input id="bannerImageUrl"  name="bannerImageUrl" style="display: none">
        图片：
        <input id="aa" type="file" class="easyui-filebox" name="bannerImage" onchange="selectImage2(this);" data-options="required:true,missingMessage:'请选择封面'"/><br/>
        <div class="lf salebd">
            <a href="#"><img id="bannerImageEcho2" src="" width="100" height="100" /></a>
        </div><br/>
        标题：<input id="bannerOldName2" name="bannerOldName"><br/>
        是否显示：<br/>显示<input id="radio1" type="radio" value="0" name="bannerDescription"><br/>
        不显示<input id="radio2" type="radio" value="1" name="bannerDescription"><br/>
    </form>
</div>
<!--修改的对话框----------------end  -->